package myJs

import japgolly.scalajs.react.vdom.all.VdomAttr
import myJs.myPkg._
import myJs.myPkg.jquery._
import org.scalajs.dom.raw.ProgressEvent
import scalatags.Text.all._

import scala.concurrent.{Future, Promise}
import scala.math.BigDecimal.RoundingMode
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import js.Dynamic.{global => g}
import myJs.tool.Tool
import myJs.tool.Tool._


/**
 * Created by yz on 2019/3/6
 */
@JSExportTopLevel("Utils")
object Utils {

  val layer = g.layer.asInstanceOf[Layer]

  val checkColumn = ColumnOptions.field("state").checkbox(true)

  val dataPercent = attr("data-percent")
  val dataShowPreview = attr("data-show-preview")
  val dataShowUpload = attr("data-show-upload")
  val dataAnimation = attr("data-animation")
  val dataField = attr("data-field")
  val dataSortable = attr("data-sortable")
  val ariaExpanded = attr("aria-expanded")
  val ariaLabelledby = attr("aria-labelledby")
  val filterDataField = attr("filter-data-field")
  val sortType = attr("sort-type")
  val dataValue = attr("data-value")
  val dataFolder = attr("data-folder")
  val dataFileName = attr("data-fileName")
  val dataName = attr("data-name")
  val dataId = attr("data-id")
  val dataRow = attr("data-row")
  val dataInputName = attr("data-inputName")
  val dataMissionKind = attr("data-missionKind")
  val align = attr("align")

  val dataContainer = VdomAttr("data-container")
  val dataHtml = VdomAttr("data-html")
  val dataDismiss = VdomAttr("data-dismiss")
  val vDataHtml = VdomAttr("data-html")
  val vDataHover = VdomAttr("data-hover")
  val vDataCloseOthers = VdomAttr("data-close-others")
  val vAriaExpanded = VdomAttr("aria-expanded")
  val dataToggle = VdomAttr("data-toggle")
  val ariaLabel = VdomAttr("aria-label")
  val dataPlacement = VdomAttr("data-placement")
  val dataContent = VdomAttr("data-content")
  val dataTrigger = VdomAttr("data-trigger")
  val dataOn = VdomAttr("data-on")
  val dataOff = VdomAttr("data-off")
  val vDataId = VdomAttr("data-id")
  val vDataValue = VdomAttr("data-value")
  val dataKind = VdomAttr("data-kind")
  val vDataInputName = VdomAttr("data-inputName")
  val vDataMissionKind = VdomAttr("data-missionKind")
  val vDataDataInputName = VdomAttr("data-dataInputName")
  val vDataGroupKind = VdomAttr("data-groupKind")
  val vDataFileName = VdomAttr("data-fileName")
  val vDataColumnNumLeast = VdomAttr("data-columnNumLeast")
  val vRef = VdomAttr("ref")

  def progressHandlingFunction = {
    (e: ProgressEvent) => {
      if (e.lengthComputable) {
        val percent = e.loaded / e.total * 100
        val newPercent = BigDecimal(percent).setScale(2, RoundingMode.HALF_UP)
        $("#progress").html(s"(${newPercent}%)")
        if (percent >= 100) {
          $("#info").text(messages("running"))
          $("#progress").html("")
        }
      }
    }

  }

  @JSExport("setColumns")
  def setColumns(value: String) = {
    val element = $(s"input:checkbox[value='${value}']")
    if (element.is(":checked")) {
      g.$("#table").bootstrapTable("showColumn", value)
    } else {
      g.$("#table").bootstrapTable("hideColumn", value)
    }

  }

  def delay(milliseconds: Int): Future[Unit] = {
    val p = Promise[Unit]()
    js.timers.setTimeout(milliseconds) {
      p.success(())
    }
    p.future
  }

  def mySetInterval(count: Int, eachF: Int => Any, endF: () => Any) = {

    def loop(f: Future[Unit], count: Int): Unit = {
      val curF = f.flatMap { x =>
        if (count == 0) {
          Future {
            endF()
            ()
          }
        } else {
          eachF(count)
          Utils.delay(1000)
        }
      }
      if (count == 0) {

      } else {
        loop(curF, count - 1)
      }
    }

    loop(Future {}, count)
  }


}
